﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class EventsDemo : Window, IWindow
    {
        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public void SetTheme()
        {
            dataGrid1.FontFamily = new FontFamily("Tahoma");
        }
  
        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }

        public EventsDemo()
        {
            InitializeComponent();

            PrepareGridData();

            dataGrid1.RowsHierarchy.CompactStyleRenderingEnabled = false;
            
            dataGrid1.ItemsSource = lst;
            dataGrid1.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ITEM_CONTENT);

            dataGrid1.ColumnsHierarchy.AllowResize = true;
            dataGrid1.ColumnsHierarchy.AllowDragDrop = true;
            dataGrid1.RowsHierarchy.AllowResize = true;
            dataGrid1.RowsHierarchy.AllowDragDrop = true;
            dataGrid1.RowsHierarchy.CompactStyleRenderingEnabled = true;

            dataGrid1.RowsHierarchy.Items[2].Expand();

            this.dataGrid1.Refresh();
            listBoxEvents.SelectionMode = SelectionMode.Single;

            #region Add event handlers
            dataGrid1.HierarchyItemExpanding += new VIBlend.WPF.Controls.DataGrid.HierarchyItemCancelEventHandler(dataGrid1_HierarchyItemExpanding);
            dataGrid1.HierarchyItemExpanded += new VIBlend.WPF.Controls.DataGrid.HierarchyItemEventHandler(dataGrid1_HierarchyItemExpanded);
            dataGrid1.HierarchyItemCollapsing += new VIBlend.WPF.Controls.DataGrid.HierarchyItemCancelEventHandler(dataGrid1_HierarchyItemCollapsing);
            dataGrid1.HierarchyItemCollapsed += new VIBlend.WPF.Controls.DataGrid.HierarchyItemEventHandler(dataGrid1_HierarchyItemCollapsed);
            dataGrid1.HierarchyItemDragStarting += new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragCancelEventHandler(dataGrid1_HierarchyItemDragStarting);
            dataGrid1.HierarchyItemDragStarted += new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragEventHandler(dataGrid1_HierarchyItemDragStarted);
            dataGrid1.HierarchyItemDragEnding += new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragCancelEventHandler(dataGrid1_HierarchyItemDragEnding);
            dataGrid1.HierarchyItemDragEnded += new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragEventHandler(dataGrid1_HierarchyItemDragEnded);
            dataGrid1.HierarchyItemMouseClick += new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_);
            dataGrid1.HierarchyItemMouseUp += new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_HierarchyItemMouseUp);
            dataGrid1.HierarchyItemMouseDown += new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_HierarchyItemMouseDown);
            dataGrid1.HierarchyItemMouseDoubleClick += new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_HierarchyItemMouseDoubleClick);

            dataGrid1.CellMouseClick += new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseClick);
            dataGrid1.CellMouseDoubleClick += new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseDoubleClick);
            dataGrid1.CellMouseDown += new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseDown);
            dataGrid1.CellMouseUp += new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseUp);
            #endregion
        }

        void dataGrid1_CellMouseUp(object sender, CellMouseButtonEventArgs args)
        {
            AddEvent("CellMouseUp: " + args.Cell.Value + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_CellMouseDown(object sender, CellMouseButtonEventArgs args)
        {
            AddEvent("CellMouseDown: " + args.Cell.Value + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_CellMouseDoubleClick(object sender, CellMouseButtonEventArgs args)
        {
            AddEvent("CellMouseDoubleClick: " + args.Cell.Value + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_CellMouseClick(object sender, CellMouseButtonEventArgs args)
        {
            AddEvent("CellMouseClick: " + args.Cell.Value + ", Button: " + args.ChangedButton.ToString());
        }

        private void AddEvent(string eventText)
        {
            listBoxEvents.SelectedIndex = -1;
            listBoxEvents.Items.Insert(0, string.Format("{0:HH:mm:ss} - {1}", DateTime.Now, eventText, eventText));
            listBoxEvents.ScrollIntoView(listBoxEvents.Items[0]);
        }

        void dataGrid1_HierarchyItemMouseDoubleClick(object sender, HierarchyItemMouseButtonEventArgs args)
        {
            AddEvent("HierarchyItemMouseDoubleClick: " + args.HierarchyItem.Text + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_HierarchyItemMouseDown(object sender, HierarchyItemMouseButtonEventArgs args)
        {
            AddEvent("HierarchyItemMouseDown: " + args.HierarchyItem.Text + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_HierarchyItemMouseUp(object sender, HierarchyItemMouseButtonEventArgs args)
        {
            AddEvent("HierarchyItemMouseUp: " + args.HierarchyItem.Text + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_(object sender, HierarchyItemMouseButtonEventArgs args)
        {
            AddEvent("HierarchyItemMouseClick: " + args.HierarchyItem.Text + ", Button: " + args.ChangedButton.ToString());
        }

        void dataGrid1_HierarchyItemDragEnded(object sender, HierarchyItemDragEventArgs args)
        {
            AddEvent("HierarchyItemDragEnded: " + args.DragSourceHierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemDragEnding(object sender, HierarchyItemDragCancelEventArgs args)
        {
            AddEvent("HierarchyItemDragEnding: " + args.DragSourceHierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemDragStarted(object sender, HierarchyItemDragEventArgs args)
        {
            AddEvent("HierarchyItemDragStarted: " + args.DragSourceHierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemDragStarting(object sender, HierarchyItemDragCancelEventArgs args)
        {
            AddEvent("HierarchyItemDragStarting: " + args.DragSourceHierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemCollapsed(object sender, HierarchyItemEventArgs args)
        {
            AddEvent("HierarchyItemCollapsed: " + args.HierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemCollapsing(object sender, HierarchyItemCancelEventArgs args)
        {
            AddEvent("HierarchyItemCollapsing: " + args.HierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemExpanded(object sender, HierarchyItemEventArgs args)
        {
            AddEvent("HierarchyItemExpanded: " + args.HierarchyItem.Text);
        }

        void dataGrid1_HierarchyItemExpanding(object sender, HierarchyItemCancelEventArgs args)
        {
            AddEvent("HierarchyItemExpanding: " + args.HierarchyItem.Text);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            this.lst.Clear();
            this.lst = null;

            #region Add event handlers
            dataGrid1.HierarchyItemExpanding -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemCancelEventHandler(dataGrid1_HierarchyItemExpanding);
            dataGrid1.HierarchyItemExpanded -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemEventHandler(dataGrid1_HierarchyItemExpanded);
            dataGrid1.HierarchyItemCollapsing -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemCancelEventHandler(dataGrid1_HierarchyItemCollapsing);
            dataGrid1.HierarchyItemCollapsed -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemEventHandler(dataGrid1_HierarchyItemCollapsed);
            dataGrid1.HierarchyItemDragStarting -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragCancelEventHandler(dataGrid1_HierarchyItemDragStarting);
            dataGrid1.HierarchyItemDragStarted -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragEventHandler(dataGrid1_HierarchyItemDragStarted);
            dataGrid1.HierarchyItemDragEnding -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragCancelEventHandler(dataGrid1_HierarchyItemDragEnding);
            dataGrid1.HierarchyItemDragEnded -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemDragEventHandler(dataGrid1_HierarchyItemDragEnded);
            dataGrid1.HierarchyItemMouseClick-= new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_);
            dataGrid1.HierarchyItemMouseUp -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_HierarchyItemMouseUp);
            dataGrid1.HierarchyItemMouseDown -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_HierarchyItemMouseDown);
            dataGrid1.HierarchyItemMouseDoubleClick -= new VIBlend.WPF.Controls.DataGrid.HierarchyItemMouseButtonEventHandler(dataGrid1_HierarchyItemMouseDoubleClick);

            dataGrid1.CellMouseClick -= new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseClick);
            dataGrid1.CellMouseDoubleClick -= new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseDoubleClick);
            dataGrid1.CellMouseDown -= new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseDown);
            dataGrid1.CellMouseUp -= new VIBlend.WPF.Controls.DataGrid.CellMouseButtonEventHandler(dataGrid1_CellMouseUp);
            #endregion
   
            this.dataGrid1.Dispose();
            this.dataGrid1 = null;
        }
        #region Sample Data

        public class Invoice
        {
            public Invoice(
                string City,
                string Region,
                string Country,
                string SalesPerson, 
                string ShipperCompany,
                string ProductName,
                double UnitPrice,
                double Quantity,
                double Discount,
                double ExtendedPrice,
                double Freight)
            {
                this.SalesPerson = SalesPerson;
                this.City = City;
                this.Region = string.IsNullOrEmpty(Region) ? "All Regions" : Region;
                this.Country = Country;
                this.ShipperCompany = ShipperCompany;
                this.ProductName = ProductName;
                this.UnitPrice = UnitPrice;
                this.Quantity = Quantity;
                this.Discount = Discount;
                this.ExtendedPrice = ExtendedPrice;
                this.Freight = Freight;
            }

              public string SalesPerson { get; set; }
              public string City { get; set; }
              public string Region { get; set; }
              public string Country { get; set; }
              public string ShipperCompany { get; set; }
              public string ProductName { get; set; }
              public double UnitPrice { get; set; }
              public double Quantity { get; set; }
              public double Discount { get; set; }
              public double ExtendedPrice { get; set; }
              public double Freight { get; set; }
        }

        public List<Invoice> lst = new List<Invoice>();

        private void PrepareGridData()
        {
            try
            {
                StreamResourceInfo sri = Application.GetResourceStream(new Uri("ExamplesCSharp;component/CSharp/DataGridViewDemos/invoices.tab", UriKind.Relative));
                System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream);
                string line = null;
                sr.ReadLine();
                while ((line = sr.ReadLine()) != null)
                {
                    string[] tabs = line.Split('\t');
                    lst.Add(new Invoice(tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], double.Parse(tabs[6]), double.Parse(tabs[7]), double.Parse(tabs[8]), double.Parse(tabs[9]), double.Parse(tabs[10])));
                }
                sr.Close();
            }
            catch (System.Exception)
            {
            }
        }
        #endregion
    }


}
